#!/sbin/bash
# Copyright (c) 2010 Andre Sencioles Vitorio Oliveira <andre@bcp.net.br>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.


### Verificar quais configuracoes ja estao aplicadas ###

# Menu option 1 - Manter Home na memoria
grep -q 'ro.HOME_APP_ADJ=1' /system/build.prop \
	&& MENU_OPT1='X' \
	|| MENU_OPT1=' '

# Menu option 2 - Mover Dalvik-Cache para /cache
grep -q 'dalvik.vm.dexopt-cache-only=1' /system/build.prop \
	&& MENU_OPT2='X' \
	|| MENU_OPT2=' '

# Menu option 3 - Problemas com DNS
grep -Eq 'net\.(.*\.)?dns[12]=' /system/build.prop \
	&& MENU_OPT3='X' \
	|| MENU_OPT3=' '


### Exibe menu ###

MENU_SELECT=$(imenu "Milestone Tools" \
	"[${MENU_OPT1}] Manter aplicativo Home sempre na memoria" \
	"[${MENU_OPT2}] Mover Dalvik-Cache para /cache" \
	"[${MENU_OPT3}] Problemas com DNS" \
	"Sair")


### Processa selecao ###

case "${MENU_SELECT}" in

	# Menu option 1 - Manter Home na memoria
	1)
		if [ "${MENU_OPT1}" == "X" ]; then
			sed -i '/ro.HOME_APP_ADJ=1/d' /system/build.prop
		else
			echo 'ro.HOME_APP_ADJ=1' >> /system/build.prop
		fi
	;;

	# Menu option 2 - Mover Dalvik-Cache para /cache
	2)
		if [ "${MENU_OPT2}" == "X" ]; then
			sed -i '/dalvik.vm.dexopt-cache-only=1/d' /system/build.prop
			rm -f /cache/dalvik-cache/*
		else
			echo 'dalvik.vm.dexopt-cache-only=1' >> /system/build.prop
			rm -f /data/dalvik-cache/*
		fi
	;;

	# Menu option 3 - Problemas com DNS
	3)
		if [ "${MENU_OPT3}" == "X" ]; then
			sed -ri '/net\.(.*\.)?dns[12]=/d' /system/build.prop
		else
			echo 'net.dns1=8.8.8.8' >> /system/build.prop
			echo 'net.dns2=8.8.4.4' >> /system/build.prop
			echo 'net.rmnet0.dns1=8.8.8.8' >> /system/build.prop
			echo 'net.rmnet0.dns2=8.8.4.4' >> /system/build.prop
		fi
	;;

	# Default - Sair do menu
	*)
		exit 0
	;;
esac

### Volta para o menu apos selecionar uma opcao ###
${0}
